<?php

/*
 * This file is part of fof/follow-tags.
 *
 * Copyright (c) FriendsOfFlarum.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Wanecho\Showip\Jobs;

use Flarum\Discussion\Discussion;
use Flarum\Notification\NotificationSyncer;
use Flarum\User\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;
use Flarum\Post\Post;
use ip2region\Ip2Region;

class IptoCityWhenPosted implements ShouldQueue
{
    use Queueable;
    use SerializesModels;

    /**
     * @var Post
     */
    protected $post;

    public function __construct(Post $post)
    {
        $this->post = $post;
    }


    public function handle()
    {
        if (!$this->post->exists) {
            return;
        }
        $this->post->ip_city = $this->getIpcity($this->post->ip_address);
        $this->post->save();
    }

    private function getIpcity($ip)
    {
        $res =  Post::where('ip_address', $ip)->first();
        if (!empty($res->ip_city)) {
            return $res->ip_city;
        }
        try {
            $region = Ip2Region::newWithVectorIndex()->search($ip);
            $ipdata = explode("|", $region);
            if (isset($ipdata[3]) && $ipdata[3] != 0) {
                return $ipdata[3];
            } else {
                return (isset($ipdata[2]) && $ipdata[2] != 0) ? $ipdata[2] : $ipdata[0];
            }
        } catch (\Exception $e) {
            return "";
        }
    }
}
